import http from '@/utils/http-util'
import ciphertext from '@/utils/ciphertext-util'

let BASE_OAUTH_CONTENT = '/oauth'
/** 登录 **/
export function loginApi(dataForm) {
  return http({
    url: BASE_OAUTH_CONTENT + '/login',
    method: 'post',
    data: http.bodyData({
      'userName': dataForm.userName,
      'password': ciphertext.encode(dataForm.password),
      'sourceType': dataForm.sourceType,
      'code': dataForm.autoLogin ? (dataForm.captcha === null || dataForm.captcha.length === 0 ? 'auto' : dataForm.captcha) : dataForm.captcha,
      'rememberMe': dataForm.autoLogin
    }, true, 'form'),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

/** 获取验证码 **/
export function getCaptchaApi(data) {
  return http.url(BASE_OAUTH_CONTENT + '/captcha?target=' + data.userName + '&sourceType=' + data.sourceType)
}

/** 刷新token **/
export function refreshTokenApi(refreshToken) {
  return http({
    url: BASE_OAUTH_CONTENT + '/refreshToken',
    method: 'put',
    data: http.bodyData({
      'refreshToken': refreshToken
    })
  })
}

/** 退出登录 **/
export function logoutApi() {
  return http({
    url: BASE_OAUTH_CONTENT + '/logout',
    method: 'get',
    params: http.queryParam()
  })
}
